import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
// import { connect } from "react-redux";

import createContainer from "./scripts/createContainer";
import handleActions from "./scripts/actions";
import createStore from "./scripts/createStore";
import handleFlows from "./scripts/flows";

const DEFAULT_OPTS = {
  target: "root",
  root: <div>Hello Rua.</div>,
  rootRouter: null,
  middleware: []
};

class Rua {
  constructor(opts) {
    this.opts = Object.assign({}, DEFAULT_OPTS, opts);
    this._models = [];
    this._app = null;
    this._actions = {};
    this._flows = {};
  }

  /**
   * 注册model
   * @param {Object} models
   */
  model(models) {
    if (!Array.isArray(models)) {
      this._models.push(models);
    } else {
      this._models = [].concat(this._models, models);
    }
  }

  /**
   * 运行Run，初始化渲染
   */
  run() {
    const { target, root, middleware } = this.opts;

    const store = createStore(this._models, middleware);
    handleActions(this._models);
    handleFlows(this._models);

    ReactDOM.render(
      <Provider store={store}>{root}</Provider>,
      document.getElementById(target)
    );
  }
}

export { createContainer };
export default Rua;
